;;; appengine-django.el --- toolkit for django

;; Copyright (C) 2011 zxy
;; Author: zxy <gcoordinate@gmail.com>
;; Maintainer: zxy <gcoordinate@gmail.com>
;; Created: Dec 2011
;; Version: 1.3.1

;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;; public var

(defvar django-lib-path nil
  "*path of django lib")

;; private var

(defvar django-server-port nil
  "*Port for app server")

;; ==================================================================
;; django-mode utility and feature defuns
;; ==================================================================

(defun* django-new-app (&optional (apppath nil) (appname nil))
  "Create new application"
  (interactive)
  ;; read information from mini buffer
  (let ((apppath (if (equal nil apppath)
                         (progn
                           (if (equal nil buffer-file-name)
                               (setq tmpapppath "~/")
                             (setq tmpapppath (file-name-directory buffer-file-name)))
                           (read-string "[appengine] Django application path: "
                                        tmpapppath nil tmpapppath nil))
                       apppath))
        (appname (if (equal nil appname)
                         (read-string "[appengine] Django application name (default demo): "
                                      nil nil "demo" nil)
                       appname)))
    (unless (file-exists-p (concat apppath"/manage.py"))
      (error "[appengine] Cannot find manage.py at %s"apppath))
    (message (shell-command-to-string (concat "python manage.py startapp "appname)))
    ))

(defun* django-new-project (&optional (projectpath nil) (projectname nil))
  "Create new project"
  (interactive)
  (unless (file-exists-p (concat django-lib-path"/bin/django-admin.py"))
    (error "[appengine] Cannot find %s!"(concat django-lib-path"/bin/django-admin.py")))
  ;; read information from mini buffer
  (let ((projectpath (if (equal nil projectpath)
                         (progn
                           (if (equal nil buffer-file-name)
                               (setq tmpprojectpath "~/")
                             (setq tmpprojectpath (file-name-directory buffer-file-name)))
                           (read-string "[appengine] Django project path: "
                                        tmpprojectpath nil tmpprojectpath nil))
                       projectpath))
        (projectname (if (equal nil projectname)
                         (read-string "[appengine] Django project name (default mysite): "
                                      nil nil "mysite" nil)
                       projectname)))
    (message (shell-command-to-string (concat django-lib-path"/bin/django-admin.py startproject "projectname)))
    (copy-directory (concat django-lib-path"/contrib/admin/media/") (concat projectpath"/media/"))
    ))

(defun* django-start-server (&optional (port "8001"))
  "Open shell to start django appserver"
  (interactive)
  (save-buffer buffer-file-name)
  (setq django-server-port port)
  (setq bufname (concat "*django-server-"port"*"))
  (if (not (eq nil (get-buffer bufname)))
      (kill-buffer bufname))
  (shell bufname)
  (comint-send-string (current-buffer) (concat "echo '[appengine] Django Server Starting...' \n"))
  (if (eq system-type 'windows-nt)
      (comint-send-string (current-buffer) (concat "python manage.py runserver "port" \n"))
    (comint-send-string (current-buffer) (concat "python manage.py runserver "port" \n")))
  (end-of-buffer)
  (other-window -1))

(defun* django-browse-appserver ()
  "Open browser to browse django server"
  (interactive)
  (browse-url (concat "http://localhost:"django-server-port)))

;;; appengine-django.el ends here